/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.coreGenerator.base.serializer.changeser.entity.serializer;

import com.inspur.edp.caf.generator.BaseGenerator;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.item.ClassGeneratorContext;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.cef.gencommon.MediateType;
import com.inspur.edp.cef.generator.coreGenerator.base.serializer.changeser.ChangeSerGenContext;
import com.inspur.edp.cef.generator.coreGenerator.base.serializer.changeser.dataType.serializer.ChangeSerClassGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.serializer.dataser.serializer.EntityDataSerGenContext;
import com.inspur.edp.cef.spi.jsonser.entity.AbstractEntityChangeSerializer;
import com.inspur.edp.das.commonmodel.IGspCommonObject;
import org.eclipse.jdt.core.dom.Modifier;

import java.util.ArrayList;
import java.util.Arrays;

public class NodeDataChangeSerClassGenerator extends ChangeSerClassGenerator {
    protected String nodeCode;
    protected boolean isRoot;
    protected NodeDataChangeSerGenContext parentSerGenContext;
    protected IGspCommonObject object;

    public NodeDataChangeSerClassGenerator(IGspCommonObject object, ChangeSerGenContext edChangeSerGenContext, NodeDataChangeSerGenContext parentSerGenContext) {
        super(object, edChangeSerGenContext);
        this.object = object;
        this.nodeCode = object.getCode();
        this.isRoot = object.getParentObject() == null;

        this.parentSerGenContext = parentSerGenContext;
    }


    @Override
    protected void beforeInitialize() {
        super.beforeInitialize();
        if (isRoot) {
            ChangeSerGenContext.setClassName(getName());
        } else {
            parentSerGenContext.getChildSerTypes().put(nodeCode, new MediateType("", getName()));
        }
    }

    protected NodeDataChangeSerGenContext getNodeDataChangeSerGenContext() {
        return (NodeDataChangeSerGenContext) super.getContext();
    }

    @Override
    protected ClassGeneratorContext createClassInfoContext() {
        return new NodeDataChangeSerGenContext(nodeCode, isRoot, ChangeSerGenContext);
    }

    @Override
    protected ArrayList<Modifier.ModifierKeyword> getAccessModifier() {
        ArrayList<Modifier.ModifierKeyword> list = new ArrayList<>();
        list.add(Modifier.ModifierKeyword.PUBLIC_KEYWORD);
        return list;
    }

    @Override
    protected String getName() {
        return String.format("%1$sChangeSerConvertor", nodeCode);
    }

    @Override
    protected TypeInfo setParentClass() {
        return new TypeInfo(AbstractEntityChangeSerializer.class.getTypeName());
    }

    @Override
    protected java.util.ArrayList<ClassMethodGenerator> createConstructorGenerators() {
        return new ArrayList<ClassMethodGenerator>(Arrays.asList(new ClassMethodGenerator[]{new NodeDataChangeSerCtorGenerator(getName(), isRoot, getNodeDataChangeSerGenContext())}));
    }

    @Override
    protected java.util.ArrayList<ClassMethodGenerator> createMethodGenerators() {
        NodeDataChangeSerGenContext nodeDataChangeSerGenContext = getNodeDataChangeSerGenContext();
        return new ArrayList<>(Arrays.asList(new ClassMethodGenerator[]
                {
                        new NodeDataSerGetTypesGenerator(object, nodeDataChangeSerGenContext.getEDSerGenContext().getMgrContext()),
                        new NodeDataChangeGetChildGenerator(nodeDataChangeSerGenContext),
                        new GetDataConvertorGenerator((EntityDataSerGenContext) nodeDataChangeSerGenContext.getEDSerGenContext().getMgrContext().getDataSerContext(),
                                nodeDataChangeSerGenContext.getIsRoot(), nodeDataChangeSerGenContext.getNodeCode())
                }));
    }

    @Override
    protected java.util.ArrayList<BaseGenerator> createClassExtendChildGenrators() {
        java.util.ArrayList<BaseGenerator> list = new java.util.ArrayList<BaseGenerator>();
        for (IGspCommonObject item : object.getContainChildObjects()) {
            list.add(getEntityChangeSerClassGenerator(item));
        }
        return list;
    }

    public NodeDataChangeSerClassGenerator getEntityChangeSerClassGenerator(IGspCommonObject item) {
        return new NodeDataChangeSerClassGenerator(item, ChangeSerGenContext, getNodeDataChangeSerGenContext());
    }
}
